<?php

namespace app\company\controller\statistics\crisis_intervention;

use app\BaseController;
use app\model\CrisisBehaviorModel;
use app\model\CrisisTypeModel;
use app\model\FollowUserModel;
use app\model\InterfereBehaviorModel;
use app\model\InterfereUserModel;
use app\model\UserModel;
use think\App;

class Index extends BaseController
{
    public function __construct(App $app)
    {
        parent::__construct($app);
    }


    /**
     * 危机人数统计
     * @return bool|string
     */
    public function NumberPeople(): bool|string
    {
        try {
            $InterfereUserModel = new InterfereUserModel();
            $data['total'] =$InterfereUserModel->where([
                ['unit_id','in',$this->userData['subset_unit']],
                ['time','>=',strtotime(date("Y-m-d"))]
            ])->distinct(true)->count('uid');
            $data['history'] = $InterfereUserModel->where([
                ['unit_id','in',$this->userData['subset_unit']]
            ])->distinct(true)->count('uid');
            return success($data);
        } catch (\Exception $e) {
            return error($e->getMessage(), $e->getCode(),[
                'line' => $e->getLine(),
                'file' => $e->getFile(),
            ]);
        }
    }

    /**
     * 危机男女比率
     * @return bool|string
     */
    public function MenWomen(): bool|string
    {
        try {
            $InterfereUserModel = new InterfereUserModel();
            $uid = $InterfereUserModel->where([
                ['unit_id','in',$this->userData['subset_unit']]
            ])->distinct(true)->column('uid');
            if (!empty($uid)){
                $data['total'] = count($uid);
                $UserModel = new UserModel();
                $data['boy'] = $UserModel->where([
                    ['id','in',$uid],
                    ['sex','=',1]
                ])->count();
                $data['girl'] = $data['total'] - $data['boy'];
            }else{
                $data['total'] = 0;
                $data['boy'] = 0;
                $data['girl'] = 0;
            }
            return success($data);
        } catch (\Exception $e) {
            return error($e->getMessage(), $e->getCode(),[
                'line' => $e->getLine(),
                'file' => $e->getFile(),
            ]);
        }
    }

    /**
     * 异常类型
     * @return bool|string
     */
    public function Exception(): bool|string
    {
        try {
            $InterfereUserModel = new InterfereUserModel();
            $InterfereBehaviorModel = new InterfereBehaviorModel();
            $CrisisBehaviorModel = new CrisisBehaviorModel();
            $Behavior = $CrisisBehaviorModel->where([
                ['unit_id','in',$this->userData['subset_unit']],
                ['status','=',1]
            ])->select()->toArray();
            $data = [];
            foreach ($Behavior as $value){
                if (empty($data[$value['name']])) {
                    $data[$value['name']] = 0;
                }
                $interfere_id = $InterfereBehaviorModel->where('behavior_id','=',$value['id'])->distinct(true)->column('interfere_id');
                $data[$value['name']] += $InterfereUserModel->where('id','in',$interfere_id)->distinct(true)->count('uid');
            }
            $data2 = [];
            foreach ($data as $key => $value){
                $data2['name'][] = $key;
                $data2['num'][] = $value;
            }
            return success($data2);
        } catch (\Exception $e) {
            return error($e->getMessage(), $e->getCode(),[
                'line' => $e->getLine(),
                'file' => $e->getFile(),
            ]);
        }
    }

    /**
     * 问题类型人数统计
     * @return bool|string
     */
    public function QuestionType(): bool|string
    {
        try {
            $CrisisTypeModel = new CrisisTypeModel();
            $InterfereUserModel = new InterfereUserModel();
            $type = $CrisisTypeModel->where([
                ['unit_id','in',$this->userData['subset_unit']],
                ['status','=',1]
            ])->select()->toArray();
            $data = [];
            $people_num = 0;
            foreach ($type as $value){
                if (empty($data[$value['name']])) {
                    $data[$value['name']] = 0;
                }
                $num = $InterfereUserModel->where([
                    ['unit_id','in',$this->userData['subset_unit']],
                    ['type_id','=',$value['id']]
                ])->distinct(true)->count('uid');
                $people_num += $num;
                $data[$value['name']] += $num;
            }
            $data2 = [];
            foreach ($data as $key => $value){
                $data2['name'][] = $key;
                $data2['num'][] = $value;
            }
            $total = $InterfereUserModel->where('unit_id','in',$this->userData['subset_unit'])->count('id');
            return success([
                'data' => $data2,
                'people_num' => $people_num,
                'total' => $total
            ]);
        } catch (\Exception $e) {
            return error($e->getMessage(), $e->getCode(),[
                'line' => $e->getLine(),
                'file' => $e->getFile(),
            ]);
        }
    }

    /**
     * 危机人员分布统计
     * @return bool|string
     */
    public function PersonnelDistribution(): bool|string
    {
        try {
            $UserModel = new UserModel();
            $InterfereUserModel = new InterfereUserModel();
            $uids = $InterfereUserModel->where('unit_id','in',$this->userData['subset_unit'])->column('uid');
            $uids = array_unique($uids);
            $user = $UserModel->where('id','in',$uids)->field('id,unit_id,sex')->with(['unitName'])->select()->toArray();
            $data = [];
            foreach ($user as $value){
                $data[$value['unit_id']][] = $value;
            }
            $data2 = [];
            foreach ($data as $item){
                $value = [
                    'name' => '',
                    'total' => 0,
                    'boy' => 0,
                    'girl' => 0,
                ];
                foreach ($item as $items){
                    $value['name'] = $items['unit_name'];
                    $value['total']++;
                    if ($items['sex'] == 1){
                        $value['boy']++;
                    }else{
                        $value['girl']++;
                    }
                }
                $data2[] = $value;
            }
            return success($data2);
        } catch (\Exception $e) {
            return error($e->getMessage(), $e->getCode(),[
                'line' => $e->getLine(),
                'file' => $e->getFile(),
            ]);
        }
    }

    /**
     * 危机干预及重点关注人员统计
     * @return bool|string
     */
    public function staff(): bool|string
    {
        try {
            $UserModel = new UserModel();
            $InterfereUserModel = new InterfereUserModel();
            $FollowUserModel = new FollowUserModel();
            //干预
            $uids = $InterfereUserModel->where([
                ['unit_id','in',$this->userData['subset_unit']]
            ])->distinct(true)->column('uid');
            $InterfereBoy = $UserModel->where([
                ['id','in',$uids],
                ['sex','=',1]
            ])->count('id');
            $InterfereGirl = count($uids) - $InterfereBoy;
            //关注
            $uids = $FollowUserModel->where([
                ['unit_id','in',$this->userData['subset_unit']]
            ])->distinct(true)->column('uid');
            $FollowBoy = $UserModel->where([
                ['id','in',$uids],
                ['sex','=',1]
            ])->count('id');
            $FollowGirl = count($uids) - $FollowBoy;
            $date = custom_time();
            $data = [];
            foreach ($date as $value){
                $data['date'][] = date('d',strtotime($value['value']));
                $data['interfere'][] = $InterfereUserModel->where([
                    ['unit_id','in',$this->userData['subset_unit']],
                    ['time','between',[strtotime($value['start_time']),strtotime($value['end_time'])]]
                ])->distinct(true)->count('uid');
                $data['follow'][] = $FollowUserModel->where([
                    ['unit_id','in',$this->userData['subset_unit']],
                    ['time','between',[strtotime($value['start_time']),strtotime($value['end_time'])]]
                ])->distinct(true)->count('uid');
            }
            return success([
                'data' => $data,
                'interfere_boy' => $InterfereBoy,
                'interfere_girl' => $InterfereGirl,
                'follow_boy' => $FollowBoy,
                'follow_girl' => $FollowGirl
            ]);
        } catch (\Exception $e) {
            return error($e->getMessage(), $e->getCode(),[
                'line' => $e->getLine(),
                'file' => $e->getFile(),
            ]);
        }
    }

}